package com.ldj.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Transaction;

//import com.ldj.hibernate.Employee;
import com.HibernateSessionFactory;
import com.ldj.common.SystemSession;
import com.ldj.hibernate.PersionFile;
import com.ldj.hibernate.UnitUser;

public class HibernateFilter implements Filter {

	private static Log log = LogFactory.getLog(HibernateFilter.class);

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		Transaction t = HibernateSessionFactory.getSession().beginTransaction();
		HttpServletRequest httprequest = (HttpServletRequest) request;
		HttpServletResponse httpresponse = (HttpServletResponse) response;
		PersionFile employee = SystemSession.getSupUserInfo(httprequest);
		UnitUser user = SystemSession.getUnitUserInfo(httprequest);
		if (("/login.do".equals(httprequest.getServletPath()) || "/register.do".equals(httprequest.getServletPath()) || "/AllToDied.do".equals(httprequest.getServletPath()))){
			try {
				chain.doFilter(request, response);
				t.commit();

			} catch (ServletException ex) {
				t.rollback();
				throw ex;
			} catch (IOException e) {
				t.rollback();
				throw e;
			} finally{
				if(HibernateSessionFactory.getSession() != null){
					HibernateSessionFactory.getSession().close();
				}
			}
		} else if (user != null
				&& ("/unitinfo.do".equals(httprequest.getServletPath())
						|| "/UnitContractInfo.do".equals(httprequest.getServletPath())
						|| "/EmployerLandtaxUpload.do".equals(httprequest.getServletPath())
						|| "/EmployerShopUpload.do".equals(httprequest.getServletPath())
						|| "/UnitContractInfoUpload.do".equals(httprequest.getServletPath()) || "/unitAnnualVeri.do"
						.equals(httprequest.getServletPath())
						|| "/logOut.do".equals(httprequest.getServletPath()) || "/unitManage.do".equals(httprequest.getServletPath())
				)) {
			try {
				chain.doFilter(request, response);
				t.commit();

			} catch (ServletException ex) {
				t.rollback();
				throw ex;
			} catch (IOException e) {
				t.rollback();
				throw e;
			} finally{
				if(HibernateSessionFactory.getSession() != null){
					HibernateSessionFactory.getSession().close();
				}
			}
		} else if (employee != null) {
			try {
				chain.doFilter(request, response);
				t.commit();
			} catch (ServletException ex) {
				t.rollback();
				throw ex;
			} catch (IOException e) {
				t.rollback();
				throw e;
			} finally{
				if(HibernateSessionFactory.getSession() != null){
					HibernateSessionFactory.getSession().close();
				}
			}
		}else{
			httpresponse.sendRedirect("error.jsp");
			t.commit();
			if(HibernateSessionFactory.getSession() != null){
				HibernateSessionFactory.getSession().close();
			}
		}
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

}
